package main

import (
	"fmt"
	"log"
	"os"
	"text/template"
)

func check(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func execuTemplate(text string, data interface{}) {
	tmpl, err := template.New("test").Parse(text)
	check(err)
	err = tmpl.Execute(os.Stdout, data)
	check(err)

}

type Part struct {
	Name   string
	Count  int
	Actice bool
}

func main() {
	templateText := "Before loop: {{.}}\n{{range .}} In loop:{{.}}\n{{end}}After loop:{{.}}\n"
	execuTemplate(templateText, []string{"hujianli1", "hujianli2", "hujianli3"})

	Price := "\nPrice is:\n{{range .}}\n${{.}}{{end}}"
	execuTemplate(Price, []float64{1.2, 1.3, 1.4, 1.5})
	execuTemplate(Price, []int{})
	execuTemplate(Price, []float64{})
	execuTemplate(Price, []string{})

	templateText2 := "Name: {{.Name}}\nCount: {{.Count}}\n"
	execuTemplate(templateText2, Part{Name: "hujianli1", Count: 120})
	execuTemplate(templateText2, Part{Name: "hujianli2", Count: 130})

	fmt.Println("------------------------------------------------------------------------------------")
	templateText3 := "Name: {{.Name}}\n{{if .Actice}}Count: {{.Count}}{{end}}\n"
	execuTemplate(templateText3, Part{Name: "hu1", Count: 1000, Actice: true})
	execuTemplate(templateText3, Part{"hu2", 100000, false}) //Name: hu2

}
